package com.mls.fivedistrict.controller.portal;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Goods;
import com.mls.fivedistrict.pojo.ShippingAddress;
import com.mls.fivedistrict.server.ShippingAddressService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-07-02
 */
@RestController("portalShippingAddressController")
@RequestMapping("/api/app")
public class ShippingAddressController {
    @Autowired
    private ShippingAddressService shippingAddressService;

    @GetMapping("/v1/shippingAddresses")
    public ServiceResponse getShippingAddress() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        Wrapper wrapper = pageFilter.getWrapper();
        if (wrapper == null) {
            wrapper = Condition.create();
        }
        wrapper.eq("user_id", RequestHolder.getCurrentUser().getId());
        List<ShippingAddress> shippingAddressList = shippingAddressService.selectList(wrapper);
        PageInfo<ShippingAddress> pageInfo = new PageInfo<>(shippingAddressList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 获取地址
     *
     * @param shippingAddressId
     * @return
     */
    @GetMapping("/v1/shippingAddress/{shippingAddressId}")
    public ServiceResponse getShippingAddressIdById(@PathVariable Long shippingAddressId) {
        Wrapper wrapper = Condition.create();
        wrapper.eq("user_id", RequestHolder.getCurrentUser().getId());
        wrapper.eq("id", shippingAddressId);
        ShippingAddress shippingAddress = shippingAddressService.selectOne(wrapper);
        if (null != shippingAddress) {
            return ServiceResponse.createSuccessResponse(shippingAddress);
        } else {
            return ServiceResponse.createErrorResponse("地址不存在!");
        }
    }

    /**
     * 修改收获地址
     *
     * @param shippingAddressId
     * @param shippingAddress
     * @return
     */
    @PutMapping("/v1/shippingAddress/{shippingAddressId}")
    public ServiceResponse editById(@PathVariable Long shippingAddressId, @RequestBody ShippingAddress shippingAddress) {
        shippingAddress.setId(shippingAddressId);
        if (shippingAddress.getDefault()) {
            List<ShippingAddress> shippingAddresses = shippingAddressService.selectList(null);
            for (ShippingAddress shipping : shippingAddresses) {
                shipping.setDefault(false);
            }
            shippingAddressService.updateBatchById(shippingAddresses);
        }
        Wrapper wrapper = Condition.create();
        wrapper.eq("user_id", RequestHolder.getCurrentUser().getId());
        wrapper.eq("id", shippingAddressId);
        boolean update = shippingAddressService.update(shippingAddress, wrapper);
        if (!update) {
            return ServiceResponse.createErrorResponse("修改失败");
        }
        return ServiceResponse.createSuccessResponse("修改成功");
    }

    /**
     * 添加收货地址
     *
     * @param shippingAddress
     * @return
     */
    @PostMapping("/v1/shippingAddress")
    public ServiceResponse insert(@RequestBody ShippingAddress shippingAddress) {
        BeanValidator.check(shippingAddress);
        shippingAddress.setUserId(RequestHolder.getCurrentUser().getId());
        if (shippingAddress.getDefault()) {
            List<ShippingAddress> shippingAddresses = shippingAddressService.selectList(null);
            for (ShippingAddress shipping : shippingAddresses) {
                shipping.setDefault(false);
            }
            shippingAddressService.updateBatchById(shippingAddresses);
        }
        boolean insert = shippingAddressService.insert(shippingAddress);
        if (!insert) {
            return ServiceResponse.createErrorResponse("插入失败");
        }
        return ServiceResponse.createSuccessResponse("插入成功");
    }

    /**
     * 删除收货地址
     *
     * @param shippingAddressId
     * @return
     */
    @DeleteMapping("/v1/shippingAddress/{shippingAddressId}")
    public ServiceResponse delete(@PathVariable Long shippingAddressId) {
//        ShippingAddress shippingAddress = new ShippingAddress();
//        shippingAddress.setId(shippingAddressId);
//        shippingAddress.setIsDelete(true);
//        boolean update = shippingAddressService.updateById(shippingAddress);
        Wrapper wrapper = Condition.create();
        wrapper.eq("user_id", RequestHolder.getCurrentUser().getId());
        wrapper.eq("id", shippingAddressId);
        boolean delete = shippingAddressService.delete(wrapper);
        if (!delete) {
            return ServiceResponse.createErrorResponse("删除失败");
        }
        return ServiceResponse.createSuccessResponse("删除成功");
    }
}

